/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","msgServ", "ajaxServ", "formServ", "bizProto","btnServ","deptSelect"],
		function($,datagridServ, msgServ, ajaxServ, formServ, bizProto,btnServ,deptSelect) {

			'use strict';

			var _bizProto,_buttonConfig,
				//锚记datagrid目标元素
				$dg,
				//初始化后的datagrid dom对象
				$grid,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,window_btn,
				//设置密码弹窗对象及按钮对象
				$password_window,password_btn,
				//业务表单对象
				$form,
				//部门选择框字段
				$deptNameField, $deptIdField,formEl, formData = 0, 
				baseEditPageUrl='/user/editPage.action',editPageUrl,
				basePasswordPageUrl='/user/userPasswordPage.action',passwordPageUrl,
				baseDelUrl = '/user/del.action',userDelUrl, 
				baseSaveUrl = '/user/save.action', userSaveUrl,
				basePasswordUrl = '/user/userPasswordReset.action',passwordSaveUrl,
				baseQueryUrl = "/user/getListDomain.action",userQueryUrl,
				baseDeptQueryUrl = "/department/getTree.action",deptQueryUrl,
				
				rootid = 'panel-user',
				
				window_id = 'user-win',
				
				window_width = 600,
				
				window_height=400,
				
				//脚本传递参数
				openParam

			var public_method = {
				
				
				init:function(doc){
					private_method.init(doc);
				},
				
				/**
				 * 脚本打开编辑页面
				 * @param _options.data	传递的数据对象
				 * @param _options.width	编辑窗口宽度
				 * @param _options.height	编辑窗口高度
				 * 
				 */
				openPage:function(_options){
					var defaultConfig ={
						width:window_width,
						height:window_height,
						
						//新增标志
						flag:false
					}
					
					
					if(_options &&　_options.data){
						openParam = _options.data
						defaultConfig.flag = true;
					}
					
					var _param = {
						grid:$('#'+rootid + ' div.dg-user'),
						window:$('#' + window_id),
						href:'/' +sysMain.getRootPath + '/user/query/editPage.action'
					}
					
					var _defaultConfig = $.extend({},defaultConfig,_options);
					datagridServ.windowEdit(_defaultConfig);
				}
				
			}

			var private_method = {
				init : function(doc) {
					if (window.console) {
					console.info('useredit modules gets ready and been loaded.');
					}
					_bizProto = new bizProto({doc:doc,rootId:'panel-user',panelSelector:'div.well',subSystemCode:'sysman',moduleCode:'user'})
					
					_buttonConfig={
						
						 "保存":private_method.submit,
						 "取消":private_method.cancel									
					}
					private_method.initPath();
					private_method.initElement(_bizProto.pageDoc)
				},
				
				//初始化路径
				initPath:function(){
					
					userSaveUrl = _bizProto.rootPath +baseSaveUrl;
					
					deptQueryUrl = _bizProto.rootPath +baseDeptQueryUrl;
					
				},
				
				
				//初始化元素
				initElement:function(doc){
					
					
					$grid = $('#'+rootid + ' div.dg-user');
					// 编辑窗口初始化
					$edit_window = $('#user-win')
					window_btn = $($edit_window).find('a.function-btn')
					$(window_btn).unbind('click')
					$(window_btn).bind('click',	private_method.function_btn_listenner)
					$form = $($edit_window).find('form')
					
					
					
					
					
					
//					// 搜索框
//					$("#searchbox").searchbox({
//						menu : "#mm",
//						prompt : '模糊查询',
//						searcher : function(value, name) {
//							var obj = {};
//							obj["searchName"]=name;
//							obj["searchValue"]=value;							
//							$dg.datagrid('reload', obj);
//						}
//
//					});				
					
					var loadedData={}
					
					var $dept = $('#' + rootid + ' div.tg-department')
					
					if($dept.length==1){
						var record = $($dept).treegrid('getSelected')
						if(record){
							loadedData.deptid = record.id
							loadedData.deptName = record.name
						}
					}
					if(openParam){
						loadedData = $.extend(loadedData,openParam)
					}else{
						var selectedRecord=$grid.datagrid('getSelected');
						if(selectedRecord ){
							loadedData = $.extend(loadedData,selectedRecord)						
						}
					}
					
					if(loadedData){
						$form.form('load',loadedData);
					}
					
					//初始化部门选择框
					$deptNameField = $($edit_window).find("#deptName")
					
					$($deptNameField).textbox({
						onClickButton:function(node){
							var _deptParam = {
								deptid:window_id + ' #deptid',								
								deptname:window_id + ' #deptName'
							}
							deptSelect.openPage(_deptParam)													
						}
					 	
					});

				},
				
				setComboValue:function(node,target){
		    		if(node!=null){		    			
		    			$(target).val(node.text);
		    		}
		    	},
				//功能按钮监听函数
				function_btn_listenner:function(e){
					
					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})
					
				},
				
				// 提交form
				submit : function() {
					
					datagridServ.save({form:$form,grid:$grid,url:userSaveUrl})					
				},				
				
				//
				cancel : function() {
					$($edit_window).window('close');
				}

			};

			
			return public_method;

		});